package com.js.myapp.system.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.js.basicutils.DialogUtil;
import com.js.basicutils.StatusBarUtil;
import com.js.basicutils.ToastUtils;
import com.js.basicutils.locate.LocationUtil;
import com.js.myapp.R;
import com.js.myapp.common.Const;
import com.js.myapp.common.activity.PrinterConnectActivity;
import com.js.myapp.system.service.LocationService;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private static final int REQUEST_SETTING_CODE = 23;

    @BindView(R.id.btn_network_setting)
    Button btnNetworkSetting;
    @BindView(R.id.btn_network_data)
    Button btnNetworkData;
    @BindView(R.id.btn_picture)
    Button btnMine;
    @BindView(R.id.btn_video)
    Button btnVideo;
    @BindView(R.id.btn_connect_printer)
    Button btnConnectPrinter;
    @BindView(R.id.btn_print)
    Button btnPrint;
    @BindView(R.id.btn_scan_qr_code)
    Button btnScanQrCode;
    @BindView(R.id.btn_common_widget)
    Button btnCommonWidget;
    @BindView(R.id.btn_blur_image)
    Button btnBlurImage;
    @BindView(R.id.btn_flow_layout)
    Button btnFlowLayout;
    @BindView(R.id.btn_refresh_layout)
    Button btnRefreshLayout;
    @BindView(R.id.btn_top_right_menu)
    Button btnTopRightMenu;

    private long firstTime;
    private MainActivity mContext;

    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarColor(this, R.color.color_statusbar_bg);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mContext = this;
        init();
        startLocationService();
    }

    private void init() {
        initDir();
    }

    private void initDir() {
        // 保存照片路径（含创建新文件夹）
        String picsDir = Const.APP_PICS;
        File file = new File(picsDir);
        if (!file.exists()) {
            file.mkdirs();// 创建文件夹
        }
        // 保存视频的路径
        String videoDir = Const.APP_VIDEOS;
        File file1 = new File(videoDir);
        if (!file1.exists()) {
            file1.mkdir();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 开启定位服务
     */
    private void startLocationService() {
        if (LocationUtil.isGPSOpen(this)) {
            Intent locationServiceIntent = new Intent(this, LocationService.class);
            startService(locationServiceIntent);
        } else {
            DialogUtil.showTipDialog(mContext, "定位功能", "是否前去开启定位功能?", "确定", new DialogUtil.DialogListener() {
                @Override
                public void onConfirm() {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivityForResult(intent, REQUEST_SETTING_CODE);
                }

                @Override
                public void onCancel() {
                }
            });
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 退出app
     */
    private void exitApp() {
        if (System.currentTimeMillis() - firstTime >= 2000) {
            ToastUtils.showOnUiThreadToast(mContext, "再按一次退出程序");
            firstTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    @OnClick({R.id.btn_network_setting, R.id.btn_network_data, R.id.btn_picture, R.id.btn_video, R.id.btn_connect_printer
            , R.id.btn_print, R.id.btn_scan_qr_code, R.id.btn_common_widget, R.id.btn_blur_image, R.id.btn_flow_layout
            , R.id.btn_refresh_layout, R.id.btn_top_right_menu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_network_setting:
                NetworkSettingActivity.start(mContext);
                break;
            case R.id.btn_network_data:
                NetWorkDataActivity.start(mContext);
                break;
            case R.id.btn_picture:
                TakePhotoActivity.start(mContext);
                break;
            case R.id.btn_video:
                TakeVideoActivity.start(mContext);
                break;
            case R.id.btn_connect_printer:
                PrinterConnectActivity.start(mContext);
                break;
            case R.id.btn_print:
                PrintActivity.start(mContext);
                break;
            case R.id.btn_scan_qr_code:
                ScanQRCodeActivity.start(mContext);
                break;
            case R.id.btn_common_widget:
                CommonWidgetActivity.start(mContext);
                break;
            case R.id.btn_blur_image:
                BlurImageActivity.start(mContext);
                break;
            case R.id.btn_flow_layout:
                FlowLayoutActivity.start(mContext);
                break;
            case R.id.btn_refresh_layout:
                SwipeRefreshLayoutActivity.start(mContext);
                break;
            case R.id.btn_top_right_menu:
                TopRightMenuActivity.start(mContext);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_SETTING_CODE:
                startLocationService();
                break;
        }
    }
}